package com.yiyi.vo;

import lombok.Data;

/**
 * 返回结果VO
 *
 * @author yiyi
 * @date 2021/6/23
 */
@Data
public class ResultVO<T> {
    private Integer code;
    private String msg;
    private T data;

    /** 成功 */
    public static final int SUCCESS = 200;

    /** 失败 */
    public static final int FAIL = 500;

    /** 创建新资源成功 */
    public static final int CREATED = 201;

    /** 删除资源成功 */
    public static final int DELETE = 204;

    /** 成功 */
    public static <T> ResultVO<T> ok()
    {
        return restResult(null, SUCCESS, null);
    }

    public static <T> ResultVO<T> ok(T data)
    {
        return restResult(data, SUCCESS, null);
    }

    public static <T> ResultVO<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    /** 失败 */
    public static <T> ResultVO<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> ResultVO<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> ResultVO<T> fail(T data)
    {
        return restResult(data, FAIL, null);
    }

    public static <T> ResultVO<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> ResultVO<T> fail(int code, String msg) { return restResult(null, code, msg); }

    /** 创建新资源成功 */
    public static <T> ResultVO<T> create() { return restResult(null, CREATED, null); }

    public static <T> ResultVO<T> create(T data) { return restResult(data, CREATED, null); }

    public static <T> ResultVO<T> create(T data, String msg) { return restResult(data, CREATED, msg); }

    /** 删除资源成功 */
    public static <T> ResultVO<T> delete() { return restResult(null, DELETE, null); }

    public static <T> ResultVO<T> delete(T data) { return restResult(data, DELETE, null); }

    public static <T> ResultVO<T> delete(T data, String msg) { return restResult(data, DELETE, msg); }

    private static <T> ResultVO<T> restResult(T data, int code, String msg) {
        ResultVO<T> result = new ResultVO<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
}
